using System;
using System.IO;
using System.Text;
namespace Genuilder.Extensions.LinqToXsd
{
	internal class Update : IDisposable
	{
		private MemoryStream stream = new MemoryStream();
		private string filename;
		private Encoding encoding;
		private TextWriter writer;
		public TextWriter Writer
		{
			get
			{
				return this.writer;
			}
		}
		public Update(string filename, Encoding encoding)
		{
			this.filename = filename;
			this.encoding = encoding;
			this.writer = new StreamWriter(this.stream, encoding);
		}
		public bool Close()
		{
			this.writer.Close();
			string memoryString = new StreamReader(new MemoryStream(this.stream.ToArray()), this.encoding).ReadToEnd();
			string fileString = "";
			using (FileStream file = new FileStream(this.filename, FileMode.OpenOrCreate))
			{
				using (StreamReader fileReader = new StreamReader(file))
				{
					fileString = fileReader.ReadToEnd();
				}
			}
			bool result;
			if (memoryString != fileString)
			{
				using (FileStream file = new FileStream(this.filename, FileMode.Create))
				{
					using (StreamWriter fileWriter = new StreamWriter(file, this.encoding))
					{
						fileWriter.Write(memoryString);
						file.SetLength(file.Position);
					}
				}
				result = true;
			}
			else
			{
				result = false;
			}
			return result;
		}
		public void Dispose()
		{
			this.Close();
		}
	}
}
